package com.fancy.service.impl;

import com.fancy.pojo.BaseResult;
import com.fancy.pojo.category.Music;
import com.fancy.pojo.dto.MusicDto;
import com.fancy.service.SearchMusicService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SearchMusicServiceImpl implements SearchMusicService {

    @Autowired
    RestHighLevelClient client;

    @Override
    public BaseResult search(String index) throws IOException {
        //DSL搜索
        SearchRequest searchRequest = new SearchRequest("music-list");

        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery(index,"musicName","musicDescription").field("musicName",10));

        //设置高亮
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
////        highlightBuilder.preTags("<font style='color:blue'>");
////        highlightBuilder.postTags("</font>");
//        highlightBuilder.fields().add(new HighlightBuilder.Field("musicName"));
//        highlightBuilder.fields().add(new HighlightBuilder.Field("musicDescription"));
//        searchSourceBuilder.highlighter(highlightBuilder);


        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest);

        SearchHits hits = search.getHits();
        SearchHit[] hits1 = hits.getHits();
        List list = new ArrayList<>();

        for (SearchHit his:hits1) {

            Map<String, Object> sourceAsMap = his.getSourceAsMap();

            MusicDto music = new MusicDto();


            String musicName=(String) sourceAsMap.get("musicName");
            String musicDescription=(String) sourceAsMap.get("musicDescription");
            Map<String, HighlightField> musicName1 = his.getHighlightFields();


            if (musicName1!=null){
                HighlightField musicName2 = musicName1.get("musicName");
                if (musicName2!=null){
                    Text[] fragments = musicName2.getFragments();
                    StringBuffer stringBuffer = new StringBuffer();
                    for (Text na: fragments) {
                        stringBuffer.append(na);
                    }
                    musicName=stringBuffer.toString();
                }

                HighlightField musicDescription1 = musicName1.get("musicDescription");
                if (musicDescription1!=null){
                    Text[] fragments = musicDescription1.getFragments();
                    StringBuffer stringBuffer = new StringBuffer();
                    for (Text na: fragments) {
                        stringBuffer.append(na);
                    }
                    musicDescription=stringBuffer.toString();
                }

            }

            music.setMusicName(musicName);
            music.setMusicDescription(musicDescription);
            music.setMusicId((Integer) sourceAsMap.get("musicId"));
            music.setMusicImage((String) sourceAsMap.get("musicImage"));
            music.setMusicUrl((String) sourceAsMap.get("musicUrl"));
            music.setMusicTypeName((String) sourceAsMap.get("musicTypeName"));
            music.setLanguageName((String) sourceAsMap.get("languageName"));

            list.add(music);
        }
        BaseResult baseResult = new BaseResult();
        baseResult.setList(list);
        baseResult.setTotal(hits.getTotalHits());

        return baseResult;
    }
}
